package com.ilovepostcard;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.ilovepostcard.controller.ShareController;
import com.ilovepostcard.controller.server.IServer;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.data.SendTaskInfo;
import com.ilovepostcard.model.Order;
import com.ilovepostcard.utils.FileLogger;

public class EmptyActivity extends BaseActivity{
	
	private long OrderID;
    private boolean isPayUri;
	private String PayUrl;
	WebView mBrowser;
	Handler mHandler;
	String currentURI;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.webbrowser);
		Intent i = getIntent();
		Bundle extras = i.getExtras();
		PayUrl = extras.getString("url");
		OrderID = extras.getLong("id");
		isPayUri = extras.getBoolean("type",false);
		mHandler = new Handler();
		mBrowser = (WebView)findViewById(R.id.web);
		currentURI = "";
		WebSettings webSettings = mBrowser.getSettings();       
        webSettings.setJavaScriptEnabled(true);       
        mBrowser.addJavascriptInterface(new Object(){       
            public void clickOnAndroid() {       
                mHandler.post(new Runnable() {       
                    public void run() {       
                    	mBrowser.loadUrl("javascript:wave()");       
                    }       
                });       
           }       
        }, "browser"); 
        
        //FileLogger.getInstance().init();
        mBrowser.setWebViewClient(new WebViewClient(){  
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
        	 currentURI = url;
        	 //Log.e("browser", "url:"+url);
        	// FileLogger.getInstance().Log("browser url:"+url);
        	 
        	String tmp = currentURI.toLowerCase();
        	if(tmp.contains("KuukieCallback.aspx") || tmp.contains("kuukiecallback.aspx") || tmp.contains("payment=success"))
     		{
        		 Handler handler = new Handler();
        		 handler.post(new Runnable()
        		 {

					@Override
					public void run() {
						IServer server = ServerFactory.getServer();
		     		    PostcardResult result = server.getOrderInfo(OrderID);
		     		    Order order = result.getOrderInfo();
		     		    if(result.isSuccess() && order!= null)
		     		    {
		     		    	SendTaskInfo task = ShareController.getInstance().getCurrentSendTask();
		     				task.orderID = order.OrderID;
		     				task.printTime = order.ShippingDate;
		     		    	Intent nextPage = new Intent(EmptyActivity.this,PrintPayConfirmActivity.class);
		     				startActivity(nextPage);
		     				finish();
		     		    }
		     		    else
		     		    {
		     		    	Toast.makeText(EmptyActivity.this, "订单取消!", Toast.LENGTH_SHORT).show();
		     		    	Intent intent = new Intent(EmptyActivity.this,MainActivity.class);
		     				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		     				startActivity(intent);
		     				finish();
		     		    }
		     		   
					}
        			 
        		 });
     			
        		 return true;
     		}
        	 else
        	 {
        		 view.loadUrl(url);  
            	 return true; 
        	 }
             
        }  
        });  
        mBrowser.loadUrl(PayUrl);       

	}
	
	public void onDestroy()
	{
		//FileLogger.getInstance().release();
		super.onDestroy();
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {  
		
		//Toast.makeText(this, "当前URI:"+currentURI, Toast.LENGTH_LONG).show();
	    if (mBrowser.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {  
	    	//mBrowser.goBack();  
	    	mBrowser.clearHistory();
    		mBrowser.goBack();
	        return true;  
	    }  

	    if(isPayUri)
	    {
		    IServer server = ServerFactory.getServer();
		    PostcardResult result = server.getOrderInfo(OrderID);
		    Order order = result.getOrderInfo();
		    if(result.isSuccess() && order!= null)
		    {
		    	SendTaskInfo task = ShareController.getInstance().getCurrentSendTask();
				task.orderID = order.OrderID;
				task.printTime = order.ShippingDate;
		    	Intent nextPage = new Intent(this,PrintPayConfirmActivity.class);
				startActivity(nextPage);
		    }
		    else
		    {
		    	Toast.makeText(this, "订单取消!", Toast.LENGTH_SHORT).show();
		    	Intent intent = new Intent(this,MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
		    }
	    }

	   return super.onKeyDown(keyCode, event);  
	}  
	
	

}
